<?php
namespace Home\Controller;
use Think\Controller;
/**
* 活动控制器
*/
class ActivityController extends Controller{
	/**
	*拉取活动列表
	*/
	public function lists(){
		$city = I('city') ? I('city') : '厦门市';
		$user_id = get_user();
		$blacks = M('black')->where(array('user_id'=>$user_id))->getField('bl_user_id',true);
		$blacks = $blacks ? $blacks : array(-1,0);
		//根据权限
		$page_no = I('post.page') ? I('post.page') : 1;
		$user = M('user')->field('lat,lng')->where('id='.$user_id)->find();
		$list = M('activity a')->page($page_no,10)
				->field("a.id,a.photo_id as aphoto_id,a.cate,a.join_ed,a.theme,a.start_time,a.bao_num,a.sex,a.site,a.join_money,a.gift,a.lng as alng,a.lat as alat,a.status,a.type,a.see_photo,b.photo_id,b.lng as blng,b.lat as blat")
				->join("left join zm_user b on a.user_id=b.id")
				->join("left join zm_who_see c on c.activity_id=a.id")
				->where(array('a.del'=>0,'a.is_pay'=>1,'a.is_remove'=>0,'a.user_id'=>array('not in',$blacks),'a.start_time'=>array('gt',time())))
				->where("(c.public_see = 1) or (c.part_see = 1 and c.right like '%,".$user_id.",%') or (c.not_see = 1 and c.right not like '%,".$user_id.",%')")
				->order('id desc')
				->select();

		$ids = array();
		$data = array();
		if(count($list) > 1){

			foreach ($list as $k => $v) {
				$ids[] = $v['id'];
			}
			$log_list = M('activity_log')
			          ->where(array('activity_id'=>array('in',$ids),'user_id'=>$user_id))
			          ->getField('activity_id,status',true);
			$log_ids = array_keys($log_list);
			//拉取到的主题要用逗号分隔开成一个数组
			foreach ($list as $key => $value) {
				$distance = getdistance($value['alng'],$value['alat'],$user['lng'],$user['lat']);
				if($distance > 50 || $value['join_ed'] > 0)
					continue;
				$list[$key]['total_money'] = ($value['join_money'] * $value['bao_num']) + $value['gift'];
				if(in_array($value['id'],$log_ids)){
					if($log_list[$value['id']] == 11){
						$list[$key]['str_status'] = "待邀约";
					}elseif($log_list[$value['id']] == 14){
						$list[$key]['str_status'] = "待签到";
					}else{
						$list[$key]['str_status'] = "报名中";
					}
				}else{

					if($value['user_id'] == $user_id){
						if($value['join_ed'] > 0){
							$list[$key]['str_status'] = "待签到";
						}else{
							$list[$key]['str_status'] = "报名中";
						}
					}else{
						if($value['join_ed'] > 0){
							$list[$key]['str_status'] = "待签到";
						}else{
							$list[$key]['str_status'] = "报名中";
						}
					}
				}
				$list[$key]['theme'] = explode(',',$value['theme']);
				$list[$key]['start_time'] = date("Y-m-d H:i",$value['start_time']);
				$list[$key]['distance'] = getdistance($value['alng'],$value['alat'],$user['lng'],$user['lat']);
				if(0 == $value['type'] && !empty($value['see_photo'])){
					if(false !== strpos($value['see_photo'], $user_id)){
						$list[$key]['type'] = 1;
					}
				}
				if ($value['user_id'] == $user_id) {
					$list[$key]['type'] = 1;
				}
				$data[] = $list[$key];
			}
		}elseif(count($list) == 1){
			
			$log_list = M('activity_log')->field('activity_id,status')->where(array('activity_id'=>$list[0]['id'],'user_id'=>$user_id))->find();
			//拉取到的主题要用逗号分隔开成一个数组
			foreach ($list as $key => $value) {
				$distance = getdistance($value['alng'],$value['alat'],$user['lng'],$user['lat']);
				if($distance > 55 || $value['join_ed'])
					continue;
				$list[$key]['total_money'] = ($value['join_money'] * $value['bao_num']) + $value['gift'];
				if($log_list['activity_id'] == $value['id']){

					if($log_list['status'] == 11){
						$list[$key]['str_status'] = "待邀约";
					}elseif($log_ids['status'] == 14){
						$list[$key]['str_status'] = "待签到";
					}else{
						$list[$key]['str_status'] = "报名中";
					}
				}else{

					if($value['user_id'] == $user_id){
						if($value['join_ed'] > 0){
							$list[$key]['str_status'] = "待签到";
						}else{
							$list[$key]['str_status'] = "报名中";
						}
					}else{
						if($value['join_ed'] > 0){
							$list[$key]['str_status'] = "待签到";
						}else{
							$list[$key]['str_status'] = "报名中";
						}
					}
				}
				$list[$key]['theme'] = explode(',',$value['theme']);
				$list[$key]['start_time'] = date("Y-m-d H:i",$value['start_time']);
				$list[$key]['distance'] = getdistance($value['alng'],$value['alat'],$user['lng'],$user['lat']);
				if ($value['user_id'] == $user_id) {
					$list[$key]['type'] = 1;
				}
				if(0 == $value['type'] && !empty($value['see_photo'])){
					if(false !== strpos($value['see_photo'], $user_id)){
						$list[$key]['type'] = 1;
					}
				}
				$data[] = $list[$key];
			}
		}
		$map['status'] = 1;
		$map['photo_id'] = array('neq','');
		$photos = M('index_photos')->where($map)->select();
		$result['photos'] =$photos;
		$result['list']=$data; 
		if (IS_POST) {
			$this->ajaxReturn($result);exit();
		}
	
	} 

	public function get_banners(){
		$map['status'] = 1;
		$map['photo_id'] = array('neq','');
		$photos = M('index_photos')->where($map)->select();
		$this->ajaxReturn($result);exit();
	}
	//首页点击地图图标，显示地图上有的活动在什么地方
	public function lists_in_map(){	
		$user_id = get_user();
		$blacks = M('black')->where(array('user_id'=>$user_id))->getField('bl_user_id',true);
		$blacks = $blacks ? $blacks : array(-1,0);
		//根据权限
		$page_no = I('post.page') ? I('post.page') : 1;
		$list = M('activity a')->page($page_no,10)
				->field("a.id,a.gift,a.type,a.title,a.theme,a.start_time,a.site,a.lng,a.lat,a.join_money,a.bao_num,a.gift,b.photo_id")
				->join("left join zm_who_see c on c.activity_id=a.id")
				->join("left join zm_user b on a.user_id=b.id")
				->where(array('a.del'=>0,'a.is_remove'=>0,'a.is_pay'=>1,'a.user_id'=>array('not in',$blacks),'start_time'=>array('gt',time())))
				->where("(c.public_see = 1) or (c.part_see = 1 and c.right like '%,".$user_id.",%') or (c.not_see = 1 and c.right not like '%,".$user_id.",%')")
				->order('id desc')
				->select();
		$user = M('user')->field("lng,lat")->where('id='.$user_id)->find();
		$returnData = array();
		$data = array();
		//拉取到的主题要用逗号分隔开成一个数组
		foreach ($list as $key => $value) {
			$list[$key]['theme'] = explode(',',$value['theme']);
			$value['total_money'] = ($value['join_money'] * $value['bao_num']) + $value['gift'];
			$value['start_time'] = date("Y-m-d H:i",$value['start_time']);
			//$list[$key]['day_time'] = date("Y-m-d",$value['start_time']);
			//比较活动的经纬度和查看人的经纬度，将活动的经纬度按照查看人的经纬度做一个4个维度的划分
			if($user['lng'] < $value['lng'] && $user['lat'] < $value['lat']){
				//第一象限
				$returnData[0]['list'][] = $value; 
			}elseif($user['lng'] > $value['lng'] && $user['lat'] < $value['lat']){
				//第二象限
				$returnData[1]['list'][] = $value; 
			}elseif($user['lng'] > $value['lng'] && $user['lat'] > $value['lat']){
				//第三象限
				$returnData[2]['list'][] = $value;
			}else{
				//第四象限
				$returnData[3]['list'][] = $value;
			}
		}
		foreach ($returnData as $key => $value) {
			if(count($value['list']) == 1){
				$returnData[$key]['more'] = false;
			}else{
				$returnData[$key]['more'] = true;
			}
		}
		foreach ($returnData as $key => $value) {
			$data[] = $value;
		}
		$this->ajaxReturn($data);
	}
	//谁可以看页面拉取数据
	public function quanxian(){
		$user_id = I('post.user_id');
		if ($user_id) {
			$this->ajaxReturn(array('status'=>0,'message'=>'请求失败'));
		}
	}
	//发起活动页面相应基础设置，比如允许报名人数，单个红包金额，见面礼红包金额等
	public function addNeedData(){
		$sitting = M('admin_setting')->where('id=1')->find();

		$yue_money = $sitting['red_num'] * $sitting['yue_bao'] + $sitting['gift'];

		$ju_money = $sitting['red_num'] * $sitting['ju_bao'] + $sitting['gift'];

		$total = $yue_money + $ju_money;

		$data = array(
			'gift'		 	=> $sitting['gift'],
			'join_money' 	=> $sitting['red_num'],
			'join_sum'		=> $sitting['join_sum'],
			'yue_sum'		=> $sitting['yue_sum'],
			'yue_money'		=> $yue_money,
			'ju_money'		=> $ju_money,
			'total'			=> $total
		);
		$this->ajaxReturn(array('status'=>1,'message'=>'请求成功','data'=>$data));
	}
	//活动添加函数
	public function add(){

		$user_id=get_user();
		if (!$user_id) {
			$this->ajaxReturn(array('status'=>0,'message'=>'请先登录'));
		}
		if(3 == $user_id){
			$this->ajaxReturn(array('status'=>0,'message'=>'客服号不能发布活动'));
		}
		$activiModel = M('activity');
		
		$_POST['start_time'] = strtotime($_POST['start_time']);
		if($_POST['start_time'] < time()+30*60){
			$this->ajaxReturn(array('status'=>0,'message'=>'活动开始时间必须在半个小时之后'));
		}
		$is_frize = M('frize')->where(array('user_id' => $user_id))->find();
		if($is_frize['status'] == 1){
			$this->ajaxReturn(array('status'=>2,'message'=>'您的账户已被冻结，请先支付诚意金'));
		}
		if($is_frize['status'] == 2){
			$this->ajaxReturn(array('status'=>0,'message'=>'您的账户已被永久冻结，不能再发布活动'));
		}
		/*//判断今天是否已经发过5次活动了，如果发过5次，就不让发
		$todayTimes = M('today_times')->where('user_id='.$user_id)->find();
		//如果有记录，则查看今天的情况
		if ($todayTimes) {
			if($todayTimes['add_time'] == strtotime(date("Y-m-d"))){
				if ($todayTimes['fa_times'] >= 5) {
					$this->ajaxReturn(array('status'=>0,'message'=>'您今天已达到最多发布次数'));
				}
			}
		}*/
		if (!$_POST) {
			$result['status'] = 0;
			$result['message'] = '参数错误';
			$this->ajaxReturn($result,"JSON");exit;
		}else{
			$user = M('user') ->field("id,sex,money,is_verify")
			                  ->where('id='.$user_id)->find();
			if ($user['is_verify'] == 0) {
				$this->ajaxReturn(array('status'=>0,'message'=>'非认证用户不能发布活动'));
			}

			$themes = explode(',',$_POST['theme']);
			$now = intval(time());
			//获取活动背景图片
			$themeTitleAndImage = getThemeImg($_POST['theme']);
			$_POST['title'] = $themeTitleAndImage['title'];
			$_POST['photo_id'] = $themeTitleAndImage['image'];
			
			$_POST['user_id'] = $user_id;
			$_POST['is_pay'] = 0;
			$start_time = $_POST['start_time'];
			$sitting = M('admin_setting')->where('id=1')->find();
			$_POST['join_money'] = $sitting['red_num'];
			$_POST['gift'] = $sitting['gift'];

			if (!$_POST['cate']) {
				$result['status']=0;$result['message']='类别不能为空';$this->ajaxReturn($result);
			}
			//如果是发的聚会，则由后台设置参加人数
			if ($_POST['cate'] == 2) {
				$_POST['people'] = $sitting['join_sum'];
				$_POST['bao_num'] = $sitting['ju_bao'];
			}else{
				$people = M('admin_setting')->where('id=1')->getField('join_sum');
				$_POST['people'] = $sitting['yue_sum'];
				$_POST['bao_num'] = $sitting['yue_bao'];
			}
			if (!$_POST['theme']) {
				$_POST['theme'] = "1";
				//$result['status']=0;$result['message']='主题不能为空';$this->ajaxReturn($result);
			}

			if (!in_array($_POST['sex'], array(0,1,2))) {
				$_POST['sex'] = $user['sex'] == 1 ? 2 : 1;
			}	
			if (!$_POST['site']) {
				$result['status']=0;$result['message']='地址不能为空';$this->ajaxReturn($result);
			}
			if (!$_POST['start_time']) {
				$result['status']=0;$result['message']='开始时间不能为空';$this->ajaxReturn($result);
			}

			$_POST['add_time'] = time();
			if (!is_numeric($_POST['who_see']['type_id'])) {
				$this->ajaxReturn(array('status'=>0,'message'=>"权限种类一定是数字"));
			}
			//根据主题自动配图片
			if ($_POST['latitude']) {
				$_POST['lat'] = $_POST['latitude'];
			}
			if ($_POST['longitude']) {
				$_POST['lng'] = $_POST['longitude'];
			}
			//发布活动要等以后支付接口完成之后再回来修改
			//$_POST['del'] = 1;
			$_POST['del'] = 0;
			$nowtime = $_POST['add_time'];
			$money = $_POST['bao_num'] * $_POST['join_money'] + $_POST['gift'];
			$model = D('Activity');
			if ($model->create()) {
	            $res_id = $model->add();
	            //谁可以看权限
	            $who_see = $_POST['who_see'];$data = array();
	            $users = $_POST['users'];
				if (!$_POST['who_see'] || $who_see == 1) {
					//如果没有传值，则默认为公开
					$data = array(
						'activity_id' => $res_id,
						'right' 	=> "0",
						'public_see'	=> 1,
						'part_see'  => 0,
						'not_see'   => 0,
					);
				}elseif($who_see == 2){
					$data = array(
						'activity_id' => $res_id,
						'right' 	=> ",".implode(",",$users).",",
						'public_see'	=> 0,
						'part_see'  => 1,
						'not_see'   => 0,
					);
					$data['right'] = $data['right'].$user_id.',';
				}else{
					$data = array(
						'activity_id' => $res_id,
						'right' 	=> ",".implode(",",$users).",",
						'public_see'	=> 0,
						'part_see'  => 0,
						'not_see'   => 1,
					);
				}
				M('who_see')->add($data);
				$dealdata = array('activity_id'=>$res_id,'start_time'=>$_POST['start_time'],'is_pay'=>0);
				M('dealing')->add($dealdata);
				$my_yu_money = M('user')->where('id='.$user_id)->getField('money');
				$this->ajaxReturn(array('status'=>1,'res_id'=>$res_id,'res_money'=>$money,'my_yu_money'=>$my_yu_money,'res_time'=>$nowtime,$ftime=>$nowtime,'message'=>"发布成功"));
	        } else {
	        	$result['status'] = 0;
	            $result['message'] = $model->getError();
	            $this->ajaxReturn($result);
	        } 
		}
	}
	//活动详情
	public function detail(){
		$lat = I('lat');
		$lng = I('lng');
		$my_id = get_user();
		//如果没有登录，则跳到注册页面
		if(!$my_id){
			$id = $_GET['id'];
			$tui_id = $_GET['tui_id'];
			"https://".C("INT_COM")."/index.php/Home/Activity/detail?&id=".$id."&tui_id=".$user_id;
			//跳转到注册页面
			if($tui_id){
				$this->redirect('Index/user_login', array('type'=>'register','id'=>$id,'tui_id'=>$tui_id));
			}else{
				$this->redirect('Index/user_login',array('type'=>'register','id'=>$id));
			}
		}
		$id = I('post.id');
		$now = time();
		$activity = M('activity')->where('id='.$id)->find();
		$user_id = $activity['user_id'];
		$userModel = M('user');
		//获取活动发起人信息
		$user = $userModel->field('id as user_id,username,is_verify,sex,age,photo_id,star,lat,lng')
							->where('id='.$user_id)->find();

		if (!$user || !$id) {
			$this->ajaxReturn(array('status'=>-1,'message'=>'参数错误'));
		}
		//判断是否有人爽约
		//后面就不用再判断是否爽约了
		if($this->isFailJoin($activity)){
			$activity = M('activity')->where('id='.$id)->find();
		}
		//活动开始了3个小时，参与人还没有领取见面礼红包
		if($activity['status'] == 13 && $activity['start_time'] < time()-3*3600){
			$res = $this->unGift($activity);
			if($res){$activity['is_gift'] = 1;}
		}
		$jump_status = 0;	
		//判断是否是自己发布的活动，若不是自己发布的活动，
		if ($user_id != $my_id) {
			$get_red = 1;
			$is_verify = $userModel->field('is_verify,photo_ok')->where('id='.$my_id)->find();//是否认证
			if($is_verify['is_verify'] == 1 && $is_verify['photo_ok'] == 1){
				$is_verify = 1;
			}else{ 
				$is_verify = 0;
			}
			//获取报名人信息
			$bm_user = $userModel->field('id as bm_user_id,username as bm_username,photo_id as bm_photo_id,age as bm_age,sex as bm_sex,star,is_verify,lat,lng,phone as bm_phone,face_token,photo_ok')->where('id='.$my_id)->find();
			if(0 == $is_verify){
				if (!$bm_user['bm_phone']) {
					$jump_status = 3;
				}elseif ($bm_user['is_verify']==0) {
					$jump_status = 4;
				}elseif (!$bm_user['bm_photo_id'] || stristr($bm_user['bm_photo_id'],'wx.qlogo') || $bm_user['photo_ok'] != 1) 
				{	
					$jump_status = 5;
				}
			}

			if(empty($bm_user['bm_sex']) || empty($bm_user['bm_age']) || empty($bm_user['star'])){
				$miss_data = 0;//表示缺少资料
			}else{
				$miss_data = 1;
			}
			$is_friend = M('contact')->where(array('user1'=>$user_id,'user2'=>$my_id))->find();
			if($is_friend){
				$bm_user['is_friend'] = 1;
			}else{
				$bm_user['is_friend'] = 0;
			}
			$activity['distance'] = getdistance($lng,$lat,$activity['lng'],$activity['lat']);
			//查看是否被冻结
			$frize = M('frize')->where('user_id='.$my_id)->find();
			if(!$frize){
				$is_frize = 0;
			}else{
				if($frize['status'] == 0){
					$is_frize = 0;
				}else{
					$is_frize = 1;
				}
			}
			//查看是否被关注
			$relations = M('follow')->where(array('user_id'=>$user_id ,'fo_user_id'=>$my_id))->find();
			//转态值查询条件
			//如果发起人已经邀约，则关闭报名
			$st_where['user_id'] = $my_id;
			$st_where['activity_id'] = $id;
			//先查找有没有记录
			$acti_log = M('activity_log')->where(array('user_id'=>$my_id,'activity_id'=>$id))->find();
			$bm_user['bm_is_invited'] = $acti_log['is_invited'];
			$bm_user['is_invited'] = $acti_log['is_invited'];
			//如果没有记录，说明没有报过名
			if (!$acti_log) {
				/* 
				*调用model的方法，精简本页面
				*/
				$status = D('Activity')->notLogStatus($activity,$relations,$is_verify);
			}elseif ($activity['status'] == 1) {
				$status = 6;
			}else{ //如果报名记录里面有记录
				//查看时候已经完成
				if($acti_log['status'] == 23){
					$status = 23;
				}
				elseif($acti_log['status'] == 16){
					//如果状态是16.说明已经确认完成，查看是否已经评价活动
					if($acti_log['is_remark'] == 0){
						$status = 19;
					}else{$status = 16;}
				}
				elseif ($acti_log['status'] == 18){
					//查看是否爽约
					$status = 18;
				}
				elseif ($activity['start_time'] < time()-3*60*60) { 
			//比如现在是10点，开始时间比7点还要早，就是活动已经开始超过三个小时了
					if ($activity['status'] == 9 || $activity['status'] == 8) {
						$status = 16;
					}elseif($activity['status'] == 1){
						$status = 6;
					}else{
						if($acti_log['is_gift']==1 || $acti_log['is_remark']==1){
							$status = 16;
						}else{
							$status = 0;
						}
					}
				}
				elseif(time() > $activity['start_time']){
					//在活动开始之后到一个半小时之内 
					//活动开始之后一个半小时到三个小时之内  ，
					//查看是是否已经确认到达，如果没有确认到达，则算是爽约,
					//同时，如果没有领取见面礼的话状态改成
					if($activity['start_time'] < time()-90*60 && $acti_log['status'] == 14 && $acti_log['is_gift'] == 0){
						//还没有领取见面礼红包，则通知领取见面了红包
						$status = 13;
					}elseif($activity['start_time'] < time()-90*60 && $acti_log['status'] == 14 && $acti_log['is_gift'] == 1){
						//领取了见面礼，则查看是否有评论
						if($acti_log['is_remark'] == 0){
							$status = 19;
						}else{$status = 16;}
					}elseif($acti_log['status'] == 14 && $acti_log['is_arrive'] == 0){
						$status = 17;
					}elseif($acti_log['status'] == 14 && $acti_log['is_arrive'] == 1){
						if($acti_log['is_gift'] == 0){
							$status = 13;
						}else{
							if($activity['is_arrive'] == 1){
								$status = 19;
							}else{
								if((time()-90*60) > $activity['start_time']){
									//活动开始1.5个小时之内,显示待完成
									$status = 22;
								}else{
									//活动开始1.5个小时之后了，对方还未到达
									//显示未完成
									$status = 21;
								}
							}
						}
						
					}elseif($acti_log['status'] == 18){  //判断爽约
						$status = 18;
					}
					elseif($activity['join_ed'] == 0){	//活动开始之后还没有邀约没有邀约则活动没完成
						$status = 0;
						M('activity')->where('id='.$id)->save(array('status'=>0));
					}
					else{
						if ($activity['status'] == 0) {//活动已经取消
							$status = 0;    //显示已过期
						}else{
							$status = 1;  
						}
					}
				}else{//活动还没有开始
					//看是否是刚申请查看头像
					if($acti_log['status'] == 2){
						$status = 2;
					}else{
						//如果活动还没有开始,查看是否报满
						if ($activity['bao_ed'] == $activity['bao_num'] || $activity['join_ed'] != 0) {
							if($acti_log['status'] == 14){
								$status = 14;
								$get_red = $acti_log['get_red'];
							}elseif($acti_log['status'] == 11){
								$status = 11;
								$get_red = $acti_log['get_red'];
							}
							else{
								$status = 4;  //已经报名，报名关闭
							}
						}elseif($activity['status'] == 1){
							//取消
							$status = 6;
						}else{
							//如果没有验证
							if($is_verify == 0){
								$status = 3;
							}else{
								if($acti_log['status'] == 7 || $acti_log['status'] == 9){
									$status = $acti_log['status'];
								}
								elseif($acti_log['status'] == 12 || $acti_log['status'] ==11 || $acti_log['status'] ==15){
									$status = $acti_log['status'];
								}elseif($acti_log['status'] == 16){
									if($acti_log['is_remark'] == 0){
										$status = 19;
									}else{
										$status = 16;
									}
								}
								elseif($acti_log['status'] == 14){
									//查看时间是否已经到了
									if ($activity['start_time'] < time()) {
										if ($acti_log['is_arrive'] == 0) {
											$status = 17;
										}else{$status = 13;}
									}else{
										$status = 14;
									}
								}else{
									$status = $acti_log['status'];
								}
							}
						}
					}
				}
			}

			$ta_com = array();$my_com = array();

			if($activity['is_arrive'] == 1){
				$user['str_status'] = "已签到";
			}else{
				if($activity['join_ed'] == 0){
					//没有邀约
					if($activity['start_time'] > time()){
						$user['str_status'] = "";
					}else{
						$user['str_status'] = "已过期";
					}
				}else{
					if($activity['start_time'] < time()-90*60){
						$user['str_status'] = "爽约";
					}else{
						$user['str_status'] = "未签到";
					}
				}
			}
			//查看是否是好友，如果不是好友，则is_friend为0
			$is_friend = M('contact')->where(array('user1'=>$my_id,'user2'=>$user_id))->find();
			$bm_user['is_friend'] = 1;
			
			if($acti_log['is_arrive'] == 1){
				if($acti_log['is_gift'] == 1){
					$bm_user['bm_str_status'] = "已领见面礼";
				}else{
					if($activity['start_time'] < time()-3*3600){
						$bm_user['bm_str_status'] = "未领见面礼";
					}else{
						$bm_user['bm_str_status'] = "已签到";
					}
				}
			}elseif($acti_log['is_invited'] == 2){
				if($activity['start_time'] < time() - 90*60){
					$bm_user['bm_str_status'] = "爽约";
				}else{
					$bm_user['bm_str_status'] = "已被邀约";
					if(!$is_friend){ $bm_user['is_friend'] = 0; }
				}
			}elseif($acti_log['is_invited'] == 1){
				if($acti_log['status'] == 23){
					$bm_user['bm_str_status'] = "已取消报名";
				}elseif($activity['join_ed'] > 0){
					$bm_user['bm_str_status'] = "未被邀约";
				}elseif($activity['start_time'] > time()){
					$bm_user['bm_str_status'] = "等待邀约";
				}else{
					$bm_user['bm_str_status'] = "未被邀约";
				}
				
			}

			if($status == 16){
				if($activity['is_remark'] == 1){
					$ta = M('comment')->where(array('activity_id'=>$id,'user_id'=>$activity['user_id']))->find();
					$ta_like = $ta['like'] ? explode(",",$ta['like']) : array();
					$ta_array = array();
					foreach ($ta_like as $ta_like_key => $ta_like_value) {
						$ta_array[] = array('text'=>$ta_like_value,'status'=>0);
					}
					$ta_like = $ta_array;
					$ta_not_like = $ta['not_like'] ? explode(",",$ta['not_like']) : array();
					$ta_not_array = array();
					foreach ($ta_not_like as $ta_not_like_key => $ta_not_like_value) {
						$ta_not_array[] = array('text'=>$ta_not_like_value,'status'=>1);
					}
					$ta_not_like = $ta_not_array;
					$ta_com = array_merge($ta_like,$ta_not_like);
				}
				$my = M('comment')->where(array('activity_id'=>$id,'user_id'=>get_user()))->find();
				$my_like = $my['like'] ? explode(",",$my['like']) : array();
				$my_array = array();
				foreach ($my_like as $my_like_key => $my_like_value) {
					$my_array[] = array('text'=>$my_like_value,'status'=>0);
				}
				$my_like = $my_array;
				$my_not_like = $my['not_like'] ? explode(",",$my['not_like']) : array();
				$my_not_array = array();
				foreach ($my_not_like as $my_not_like_key => $my_not_like_value) {
					$my_not_array[] = array('text'=>$my_not_like_value,'status'=>1);
				}
				$my_not_like = $my_not_array;
				$my_com = array_merge($my_like,$my_not_like);
			}
			//当状态值是11时，需要显示一起参加活动的人有哪些
			if (in_array($status, array(11,13,14,15,16,18,19,22))) {
				$st_where2['activity_id'] = $id;
				$st_where2['is_invited'] = array('neq',0);
				$list = M('activity_log a')
					->field('a.is_invited,b.id,b.username,b.sex,b.age,b.photo_id')
					->join('left join zm_user b on a.user_id=b.id')
					->where($st_where2)->select();
				
				if ($list) {
					$activity['start_time']=date("Y-m-d H:i",$activity['start_time']);
					$result = array('is_self'=>0,'is_verify'=>$is_verify,'miss_data'=>$miss_data,'is_frize'=>$is_frize,'status'=>$status,'list'=>$list,'user'=>$user,'bm_user'=>$bm_user,'my_com'=>$my_com,'get_red'=>$acti_log['get_red'],'ta_com'=>$ta_com,'activity'=>$activity,'is_gift'=>$activity['is_gift'],'jump_status'=>$jump_status);
					$this->ajaxReturn($result,"JSON");
				}else{
					$activity['start_time']=date("Y-m-d H:i:s",$activity['start_time']);
					$list = array();
					$result = array('is_self'=>0,'is_verify'=>$is_verify,'miss_data'=>$miss_data,'is_frize'=>$is_frize,'my_com'=>$my_com,'ta_com'=>$ta_com,'status'=>$status,'list'=>$list,'get_red'=>$acti_log['get_red'],'user'=>$user,'bm_user'=>$bm_user,'activity'=>$activity,'is_gift'=>$activity['is_gift'],'jump_status'=>$jump_status);
					$this->ajaxReturn($result,"JSON");
				}
			}

			$activity['start_time']=date("Y-m-d H:i",$activity['start_time']);
			$result = array('is_self'=>0,'is_verify'=>$is_verify,'miss_data'=>$miss_data,'is_frize'=>$is_frize,'status'=>$status,'user'=>$user,'bm_user'=>$bm_user,'activity'=>$activity,'my_com'=>$my_com,'get_red'=>$acti_log['get_red'],'ta_com'=>$ta_com,'list'=>array(),'is_gift'=>$activity['is_gift'],'jump_status'=>$jump_status);
			$this->ajaxReturn($result,"JSON");
		}else
		{
			//若是自己发布的活动
			$activity['distance'] = getdistance($lng,$lat,$activity['lng'],$activity['lat']);
			if (!$user || !$id) {
				$result['status'] = -1;
				$result['message'] = '参数错误';
				$this->ajaxReturn($result,"JSON");
			}
			if ($activity['is_remove'] == 1 || $activity['status'] == 1) {
				//如果是取消了
				$status = 1;$list=array();
			}elseif($activity['bao_ed'] == 0 && $activity['start_time'] < time()){
				/*活动已经开始了，
				*还没有人报名,活动过期
				*将用户的报名红包和见面礼红包返还给活动发起人
				*/
				$status = D('Activity')->ReturnMoneyToDatedActivi($activity);
				$list = array();
			}elseif($activity['join_ed'] == 0 && $activity['start_time'] < time()){
				//活动已经开始了，有人报名了但是没有被邀约，需要让报名的人得到红包
				//并且剩下的红包退回给发起人
				$status = D('Activity')->ReTurnGiftToDatedActivity($activity);
				$list = array();
			}
			else{
				//除开过期和取消以外，若没人参加活动
				if ($activity['bao_ed'] == 0) {
					$status = D('Activity')->StatusOfNoneJoin($activity);
					$list = array();
				}
				else{
					//如果已经有人来参加报名了，则要往前端返回参加人
					//如果时间还没到，则返回6，显示确认用户
					$st_where2['activity_id'] = $id;
					$st_where2['status'] = array('in',array(11,12,13,14,15,16,18,22));
					$list = D("Activity")->getBmUserList($st_where2);
					if(time() < $activity['start_time']){
						//有人报名并且时间没到
						//如果活动还没有开始，判断是否邀约了活动参与人
						if ($activity['join_ed'] > 0) {
							$status = 7;
						}else{
							$status = 6;
							$st_where2['activity_id'] = $id;
							$st_where2['status'] = array('in',array(11,13,14,15,16));

							$list = D("Activity")->getBmUserList($st_where2);

							foreach ($list as $key => $value) {
								if($value['last_time']){
									$list[$key]['remain_minutes'] = intval(($value['last_time']-time())/60);
								}else{
									$list[$key]['remain_minutes'] = intval(($activity['start_time']-time())/60);
								}
								
							}
						}
					}else
					{	//time() > $activity['start_time']
						//活动已经开始
						//活动开始分为开始到8:30和开始到9点
						if ($activity['status'] == 7) {  //由于一开始就判断是否爽约了，所以这个地方不用管爽约的事
							if ($activity['is_arrive'] == 0) {
								$status = 10;
							}else{
								//自己已经到达现场
								//判断对方有没有到现场，没有到现场则显示待完成
								$joinuser = M('activity_log')->where(array('activity_id'=>$id,'is_invited'=>2))->find();
								if($joinuser['is_arrive'] == 0){
									$status =7;
								}else{
									$status =8;
								}
							}
						}
						else{
							$status = $activity['status'];
						}
					}
				} 
			}
			
			$list = D('Activity')->dealBaomingList($list,$activity);

			//如果状态是2，查看是否有人请求关注
			if ($status == 2 || $status == 6 || $status == 3 || $status == 4) {
				$res = D("Activity")->getFollowAndPhoto($id);
				if ($res) {
					//提示前端要弹出请求的弹窗
					$is_askfor = 1;
					$askfor_array = $res;
				}
				else{
					$is_askfor = 0;
					$askfor_array = array();
				}
			}else{
				$is_askfor = 0;
					$askfor_array = array();
			}
			$ta_com = array();$my_com = array();

			if($status == 9 || $status == 8 || $status == 11 || $status == 13){
				if($status == 13){
					if($activity['start_time'] < (time()-90*60)){
						$status = 11;
					}
				}
				$bm_log = M('activity_log')->where(array('activity_id'=>$id,'is_invited'=>2))->find();
				if($activity['is_arrive'] == 1){
					$user['str_status'] = "已签到";
				}else{
					if($activity['status'] == 11){
						$user['str_status'] = "爽约";
					}
				}
				if($bm_log){
					$bm_user = M('user')
					     ->field("photo_id as bm_photo_id,id as bm_user_id,username as bm_username")
					     ->where('id='.$bm_log['user_id'])->find();
					if($bm_user){
						$bm_user['bm_str_status'] = getBmStrStatus($bm_log);
					}
				}
				
				if($activity['is_remark'] == 1){
					
					$ta = M('comment')->where(array('activity_id'=>$id,'to_user_id'=>$activity['user_id']))->find();
					$ta_like = $ta['like'] ? explode(",",$ta['like']) : array();
					$ta_array = array();
					foreach ($ta_like as $ta_like_key => $ta_like_value) {
						$ta_array[] = array('text'=>$ta_like_value,'status'=>0);
					}
					$ta_like = $ta_array;
					$ta_not_like = $ta['not_like'] ? explode(",",$ta['not_like']) : array();
					$ta_not_array = array();
					foreach ($ta_not_like as $ta_not_like_key => $ta_not_like_value) {
						$ta_not_array[] = array('text'=>$ta_not_like_value,'status'=>1);
					}
					$ta_not_like = $ta_not_array;
					$ta_com = array_merge($ta_like,$ta_not_like);
				}
				
				$my = M('comment')->where(array('activity_id'=>$id,'user_id'=>$activity['user_id']))->find();
				$my_like = $my['like'] ? explode(",",$my['like']) : array();
				$my_array = array();
				foreach ($my_like as $my_like_key => $my_like_value) {
					$my_array[] = array('text'=>$my_like_value,'status'=>0);
				}
				$my_like = $my_array;
				$my_not_like = $my['not_like'] ? explode(",",$my['not_like']) : array();
				$my_not_array = array();
				foreach ($my_not_like as $my_not_like_key => $my_not_like_value) {
					$my_not_array[] = array('text'=>$my_not_like_value,'status'=>1);
				}
				$my_not_like = $my_not_array;
				$my_com = array_merge($my_like,$my_not_like);
			}
			
			if($status == 9){
				$status = ($activity['is_remark'] == 1) ? 9 : 12;
			}
			$activity['start_time']=date("Y-m-d H:i:s",$activity['start_time']);
			if(!$bm_user){
				$bm_user=null;
			}else{
				$acti_log = M('activity_log')->where(array('activity_id'=>$id,'is_invited'=>2))->find();
				if($acti_log['is_arrive'] == 1){
					if($acti_log['is_gift'] == 1){
						$bm_user['bm_str_status'] = "已领见面礼";
					}else{
						if($activity['start_time'] < time()-3*3600){
							$bm_user['bm_str_status'] = "未领见面礼";
						}else{
							$bm_user['bm_str_status'] = "已签到";
						}
					}
				}elseif($acti_log['is_invited'] == 2){
					if($activity['start_time'] < time() - 90*60){
						$bm_user['bm_str_status'] = "爽约";
					}else{
						$bm_user['bm_str_status'] = "已被邀约";
					}
				}elseif($acti_log['is_invited'] == 1){
					if($activity['start_time'] > time()){
						$bm_user['bm_str_status'] = "待邀约";
					}else{
						$bm_user['bm_str_status'] = "未被邀约";
					}
					
				}
			}
			//$list[0]['str_status'] = $bm_user['bm_str_status'];
			$result = array(
				'is_askfor'=> $is_askfor,
				'askfor_array'=>$askfor_array,
				'is_self' => 1,
				'status'  => $status,
				'user'    => $user,
				'bm_user' => $bm_user,
				'ta_com'  => $ta_com,
				'my_com'  => $my_com,
				'activity'=> $activity,
				'list'    => $list,
				'miss_data'=> 0,
				'is_frize'=> 0,
				'is_gift'=>$activity['is_gift']
			);
			$this->ajaxReturn($result,"JSON");
		}
	}
	//因为没有领取见面礼没有得到见面礼红包
	public function unGift($activity){
		
		$log = M('activity_log')->where('activity_id='.$activity['id'].' AND is_invited=2')->find();
		if($log['is_arrive'] == 1){
			M('activity')->where('id='.$activity['id'])->save(array('status'=>9,'is_remark'=>1,'is_gift'=>0));
			M('activity_log')->where('id='.$log['id'])->save(array('status'=>16,'is_remark'=>1));
			return true;
		}
		return false;
	}
	//请求查看头像
	public function allowPhoto(){
		$id = I('post.id');		//活动id
		$user_id = get_user(); //得到session储存的用户id
		if (!$id || !$user_id) {
			$this->ajaxReturn(array('status'=>0,'message'=>'参数错误'));
		}
		$activi = M('activity')->where('id='.$id)->find();
		$user = M('user')->field('id,username,is_verify')->where('id='.$user_id)->find();
		if(0 == $user['is_verify']){
			$this->ajaxReturn(array('status'=>0,'message'=>'非认证用户不能发布活动'));
		}
		$dataArray = array(
			'user_id' => $user_id,
			'activity_id'=>$id, 
		);
		$ac_log = M('activity_log')->where($dataArray)->find();
		//若已经存在记录，则参数错误
		if ($ac_log) {
			$this->ajaxReturn(array('status'=>0,'message'=>'已请求查看资料，请耐心等待'));
		}
		$dataArray['add_time'] = time();
		$dataArray['status'] = 2;
		//向活动记录表总添加活动参与记录
		$res = M('activity_log')->add($dataArray);
		if (!$res) {
			$this->ajaxReturn(array('status'=>0,'message'=>'请求查看失败'));
		}
		$now = time();
		//添加通知消息
		$content = $user['username']."请求查看资料！";
		D('Message')->addSysMsg($activi['user_id'],$content,$id);
		$msg = array(
			'fa_id'		=> 0,
			'shou_id'	=> $activi['user_id'],
			'content'	=> $content,
			'add_time'	=> $now,
			'type'		=> 0,
			'interval'  =>  intval(time()/(5*60)),
			'url'  		=> 'http://'.$_SERVER['SERVER_NAME'].'/index.php/Home/Index/user_detail.html?user_id='.$user_id.'&is_askfor=1&ask_status=8&username='.$user['username'].'&activity_id='.$id
			);
		//$res = M('message')->add($msg);
		//D('Message')->messageSystemList($activi['user_id'],$content,$now);
		$this->ajaxReturn(array('status'=>1,'message'=>'请求查看成功，等待对方同意'));
	}
	
	//搜索
	public function search(){
		$user_id = get_user();
		$page_no = I('post.page') ? I('post.page') : 1;
		$blacks = M('black')->where(array('user_id'=>$user_id))->getField('bl_user_id',true);
		$blacks = $blacks ? $blacks : array(-1,0);
		$where = array();
		if (I('post.cate')) {
			$where['cate'] = I('post.cate');
		}
		if (I('post.theme')) {
			$where['theme'] = I('post.theme');
		}
		if (I('post.sex')) {
			$sex = I('post.sex');
			if(1 == $sex){
				$where['a.sex'] = array('neq',2);
			}elseif(2 == $sex){
				$where['a.sex'] = array('neq',1);
			}
		}
		$type = I('post.type'); 
		$lat = I('post.lat');
		$lng = I('post.lng');
		if(2 == $type && !empty($lat) && !empty($lng)){

			$order = "ACOS(SIN((".$lat." * 3.1415) / 180 ) *SIN((a.lat * 3.1415) / 180 ) +COS((".$lat." * 3.1415) / 180 ) * COS((a.lat * 3.1415) / 180 ) *COS((".$lng." * 3.1415) / 180 - (a.lng * 3.1415) / 180 ) ) * 6380 asc";

			$list = M('activity a')->page($page_no,10)
				->field("a.id,a.cate,a.photo_id as aphoto_id,a.theme,a.drink,a.start_time,a.sex,a.site,a.right,a.join_money,a.bao_num,a.gift,a.status,a.type,a.lat,a.lng,b.photo_id")
				->join("left join zm_user b on a.user_id=b.id")
				->join("left join zm_who_see c on c.activity_id=a.id")
				->where($where)
				->where(array('a.del'=>0,'is_pay'=>1,'a.user_id'=>array('not in',$blacks),'start_time'=>array('gt',time())))
				->where("(c.public_see = 1) or (c.part_see = 1 and c.right like '%,".$user_id.",%') or (c.not_see = 1 and c.right not like '%,".$user_id.",%')")
				->order($order)
				->select();
		}else{
			$list = M('activity a')->page($page_no,10)
				->field("a.id,a.cate,a.photo_id as aphoto_id,a.theme,a.drink,a.start_time,a.sex,a.site,a.right,a.join_money,a.bao_num,a.gift,a.status,a.type,a.lat,a.lng,b.photo_id")
				->join("left join zm_user b on a.user_id=b.id")
				->join("left join zm_who_see c on c.activity_id=a.id")
				->where($where)
				->where(array('a.del'=>0,'is_pay'=>1,'a.user_id'=>array('not in',$blacks),'start_time'=>array('gt',time())))
				->where("(c.public_see = 1) or (c.part_see = 1 and c.right like '%,".$user_id.",%') or (c.not_see = 1 and c.right not like '%,".$user_id.",%')")
				->order('start_time desc')
				->select();
		}
		
		foreach ($list as $key => $value) {
			$list[$key]['str_status'] = "进行中";
			$list[$key]['total_money'] = $value['gift'] + ($value['join_money'] * $value['bao_num']);
			$list[$key]['theme'] = explode(',',$value['theme']);
			$list[$key]['start_time'] = date("Y-m-d H:i",$value['start_time']);
			$list[$key]['distance'] = getdistance($value['lng'],$value['lat'],$lng,$lat);
		}
		$photos = M('index_photos')->where('status=1')->select();
		$result['photos'] =$photos;
		$result['list']=$list; 
		if (IS_POST) {
			$this->ajaxReturn($result);exit();
		}
	}
	//请求关注
	public function allowFollow(){
		$id = I('post.id');  //活动id
		$user_id = get_user();  //当前用户id
		if (!$id || !$user_id) {
			$this->ajaxReturn(array('status'=>0,'message'=>'参数错误'));
		}
		$activiModel = M('activity');
		$logModel = M('activity_log');
		//查找活动详情
		$activi = $activiModel->where('id='.$id)->find();
		//先判断有没有关注对方，如果没有关注对方先关注
		$followTa = M('follow')->where(array('user_id'=>$user_id,'fo_user_id'=>$activi['user_id']))->find();
		if(!$followTa){
			M('follow')->add(array('user_id'=>$user_id,'fo_user_id'=>$activi['user_id'],'add_time'=>time()));
			
		}
		//查找对方是否已经关注了我
		$is_follow = M('follow')->where(array('user_id'=>$activi['user_id'],'fo_user_id'=>$user_id))->find();
		//查找我的用户名
		$username = M('user')->where('id='.$user_id)->getField("username");
		//查找是否有记录
		$log_ac = $logModel->where(array('user_id'=>$user_id,'activity_id'=>$id))->find();
		//如果有记录并且记录里面已经有请求了，则返回
		if($log_ac){
			//如果有记录且已经关注了
			if ($is_follow) {
				if ($log_ac['status'] == 9) {
					//已关注，请刷新页面
					$this->ajaxReturn(array('status'=>1,'message'=>'已关注，请刷新页面'));
				}
				if ($log_ac['status'] == 7 || $log_ac['status'] == 8) {
					//将记录的状态改成9，返回1，成功
					$logModel->where(array('user_id'=>$user_id,'activity_id'=>$id))->save(array('status'=>9));
				}else{
					$this->ajaxReturn(array('status'=>0,'message'=>'失败，请刷新页面'));
				}
			}
			else{ //正常情况之一，有记录但是还没有关注
				//将
				//如果状态的值为8，则已经请求过了
				if ($log_ac['status'] == 8) {
					$this->ajaxReturn(array('status'=>0,'message'=>'已请求关注，请耐心等待'));
				}
				if($log_ac['status'] == 7){
					//如果状态为7，则需要请求关注
					$logModel->where(array('user_id'=>$user_id,'activity_id'=>$id))->save(array('status'=>8));
					$content = $username."请求关注！";
					D('Message')->addSysMsg($activi['user_id'],$content,$id);
					$this->ajaxReturn(array('status'=>1,'message'=>'请求成功'));
				}
			}	
		}
		else{
			if ($is_follow) {
				//已经关注了，添加记录，status = 9
				$data = array(
					'user_id'		=> $user_id,
					'activity_id'	=> $id,
					'add_time'		=> time(),
					'status'		=> 9,
				);
				$res = $logModel->add($data);
				$this->ajaxReturn(array('status'=>1,'message'=>'对方已经关注你，请刷新网页报名'));
			}
			else{
				$data = array(
					'user_id'		=> $user_id,
					'activity_id'	=> $id,
					'add_time'		=> time(),
					'status'		=> 8,
				);
				$res = $logModel->add($data);
				$content = $username."请求关注！";
				D('Message')->addSysMsg($activi['user_id'],$content,$id);
				$this->ajaxReturn(array('status'=>1,'message'=>'请求成功,请耐心等待对方关注'));
			}
		}
	}
	//同意被关注  //这个地方要测试一下
	public function yesFollow(){
		$id = I('post.id');
		$user_id = I('post.user_id');
		$my_id = get_user();
		if (!$user_id || !$my_id) {
			$this->ajaxReturn(array('status'=>0,'message'=>'参数错误'));
		}
		//$activi = M('activity')->where('id='.$id)->find();
		$username = M('user')->where('id='.$my_id)->getField("username");
		$logModel = M('activity_log');
		$data = array(
			'user_id' => $my_id,
			'fo_user_id' => $user_id,
		);
		//查找是否已经关注
		$is_exist = M('follow')->where($data)->find();
		if ($is_exist) {
			//如果已经关注了，本记录状态值要改成9
			$logModel->where(array('user_id'=>$user_id,'status'=>array('in',array(7,8))))->save(array('status'=>9));
			$this->ajaxReturn(array('status'=>'1','message'=>'已成功关注'));
		}
		$data['add_time'] = time();
		$res = M('follow')->add($data);
		if (!$res) {
			$this->ajaxReturn(array('status'=>'0','message'=>'关注失败'));
		}

		$ids = M('activity')->where(array('user_id'=>$my_id))->getField('id',true);
		if (count($ids) > 1) {
			$logModel->where(array('user_id'=>$user_id,'status'=>array('in',array(7,8)),'activity_id'=>array('in',$ids)))->save(array('status'=>9));
		}else{
			$logModel->where(array('user_id'=>$user_id,'status'=>array('in',array(7,8)),'activity_id'=>$ids[0]))->save(array('status'=>9));
		}
		//添加提示消息，
		$content = $username."已同意你关注他了！";
		D('Message')->addSysMsg($user_id,$content,$id);
		
		$this->ajaxReturn(array('status'=>'1','message'=>'关注成功'));
	}
	//同意查看头像
	public function yesPhoto(){
		$user_id = I('user_id');
		$my_id = get_user();
		$id = I('post.id');
		if (!$id || !$user_id || !$my_id) {
			$this->ajaxReturn(array('status'=>0,'message'=>'参数错误'));
		}
		//$activi = M('activity')->where('id='.$id)->find();
		$userModel = M('user');
		$user = $userModel->field('id,username,is_verify')->where('id='.$user_id)->find();
		//发起人用户名
		$username = $userModel->where('id='.$my_id)->getField('username');
		//查看是否关注
		$is_exist = M('follow')->where(array('user_id'=>$my_id,'fo_user_id'=>$user_id))->find();
		
		if($user['is_verify'] == 0){
			//如果还没有验证，要先验证
			M('activity_log')->where(array('user_id'=>$user_id,'status'=>2,'activity_id' => $id))->save(array('status'=>3));
		}
		elseif ($is_exist) {
			//如果关注了好友，则可以直接报名
			M('activity_log')->where(array('user_id'=>$user_id,'status'=>2,'activity_id' => $id))->save(array('status'=>9));
		}
		else{
			//如果没有关注好友，则需要先关注
			M('activity_log')->where(array('user_id'=>$user_id,'status'=>2,'activity_id' => $id))->save(array('status'=>7));
		}
		$see_photo = M('activity')->where('id='.$id)->getField('see_photo');

		if(empty($see_photo)){
			M('activity')->where('id='.$id)->save(array('see_photo'=>$user_id));
		}else{
			M('activity')->where('id='.$id)->save(array('see_photo'=>$see_photo.','.$user_id));
		}
		//添加提示消息，
		$content = $username."已同意你查看他的资料了！";
		D('Message')->addSysMsg($user_id,$content,$id);
		$this->ajaxReturn(array('status'=>'1','message'=>'同意查看头像成功'));
	}
	//不同意查看头像 
	public function notPhoto(){
		$user_id = I('user_id');
		$id = I('id');
		if (!$user_id || !$id) {
			$this->ajaxReturn(array('status'=>'-1','message'=>'参数错误'));
		}
		//将not_photo字段改成1，表示不允许查看头像
		$res = M('activity_log')->where(array('user_id'=>$user_id,'activity_id'=>$id))->save(array('status'=>2,'not_photo'=>1));
		if ($res===false) {
			$this->ajaxReturn(array('status'=>'0','message'=>'操作失败'));
		}
		$this->ajaxReturn(array('status'=>'1','message'=>'操作成功'));
	}
	//不同意关注
	public function notFollow(){
		$user_id = I('user_id');
		$id = I('id');
		if (!$user_id || !$id) {
			$this->ajaxReturn(array('status'=>'-1','message'=>'参数错误'));
		}
		$res = M('activity_log')->where(array('user_id'=>$user_id,'activity_id'=>$id))->save(array('status'=>8,'not_photo'=>1));
		if ($res===false) {
			$this->ajaxReturn(array('status'=>'0','message'=>'操作失败'));
		}
		$this->ajaxReturn(array('status'=>'1','message'=>'操作成功'));
	}
	//报名
	public function baoming(){
		$id = I('post.id');  //活动id
		$user_id = get_user();  //当前用户id
		$activiModel = M('activity');
		$logModel = M('activity_log');
		$activi = $activiModel->where('id='.$id)->find();  //查找活动
		if (!$id || !$user_id || !$activi) {
			$this->ajaxReturn(array('status'=>0,'message'=>'参数错误'));
		}
		$userModel = M('user');
		$user = $userModel->field('is_verify,username,sex,age')->where('id='.$user_id)->find();
		if(!$user['age'] || !$user['sex']){
			$this->ajaxReturn(array('status'=>3,'message'=>'请先完善资料，如年龄、性别等'));
		}
		if($activi['sex'] != 0){
			if($activi['sex'] != $user['sex']){
				$this->ajaxReturn(array('status'=>0,'message'=>'发布者指定了其他性别参与者'));
			}
		}
		if($activi['start_time'] < time())
			$this->ajaxReturn(array('status'=>0,'message'=>'活动已过期'));
		$joins_ids = $logModel->where(array('user_id'=>$user_id,'is_invited'=>1,'activity_id'=>array('neq'=>$id),'add_time'=>array('gt',time()-12*3600)))->getField('activity_id',true);
		if(!empty($join_ids)){
			$has_join = $activiModel->where(array('id'=>array('in',$joins_ids),'start_time'=>array('gt',time()-12*3600)))->find();
			if($has_join){
				$this->ajaxReturn(array('status'=>0,'message'=>'您已经报名参加了该发起者的其他活动，请选择其他活动报名'));
			}
		}
		
		//判断用户是否被冻结
		$is_frize = M('frize')->where(array('user_id'=>$user_id))->find();
		if ($is_frize && $is_frize['status']!=0) {
			if ($is_frize['status'] == 1) {
				$this->ajaxReturn(array('status'=>0,'message'=>'您的账户暂时被冻结，请先解冻'));
			}else{
				$this->ajaxReturn(array('status'=>0,'message'=>'您的账户已被冻结'));
			}
		}
		//查看用户是否报名次数达到5次
		$todayTimes = M('today_times')->where('user_id='.$user_id)->find();
		$most_times = M('admin_setting')->where('id=1')->getField('bao_times');
		if(!$most_times){
			$most_times = 5;
		}
		//如果有记录，则查看今天的情况
		if ($todayTimes) {
			if($todayTimes['add_time'] == strtotime(date("Y-m-d"))){
				if ($todayTimes['bao_times'] >= $most_times) {
					$this->ajaxReturn(array('status'=>0,'message'=>'您今天已达到最多报名次数'));
				}
			}
		}
		//查找用户是否参加了同一时段的其他活动，如果参加了，就不能再参加这个活动了。
		$join_map['activity_id'] = array('neq',$id);
		$join_map['add_time'] = array('gt',strtotime(date("Y-m-d 6:00:00")));
		$join_map['is_invited'] = 2;
		$join_map['user_id'] = $user_id;
		$is_join = $logModel->where($join_map)->select();
		if ($is_join) {
			foreach ($is_join as $key => $value) {
				$join_activi = M('activity')->where('id='.$value['activity_id'])->find();
				if($join_activi['is_remove'] == 0){
					$this->ajaxReturn(array('status'=>'0','message'=>'您已经报名参加了本时段的其他活动，不允许重复报名'));
				}
			}
		}
		//检查是否还有报名名额
		if ($activi['bao_num'] <= $activi['bao_ed']) {
			$this->ajaxReturn(array('status'=>'0','message'=>'您的动作太慢了，名额已报满。看看别的活动吧'));
		}
		$now = time();
		//查看是否是认证用户
		
		//不是认证用户，不允许报名
		if ($user['is_verify'] == 0) {
			$this->ajaxReturn(array('status'=>'0','message'=>'不是认证用户，不允许报名'));
		}
		$money = $activi['join_money'];
		//如果已经邀约，则报名关闭
		if($activi['join_ed'] != 0){
			$this->ajaxReturn(array('status'=>'0','message'=>'报名已关闭'));
		}
		//查找是否被关注
		$fo = M('follow')->where(array('user_id'=>$activi['user_id'],'fo_user_id'=>$user_id))->find();
		if (!$fo) {
			//如果没有关注，则需要先请求关注
			$this->ajaxReturn(array('status'=>0,'message'=>'请先请求关注'));
		}
		//先查找是否有报名记录
		$data['activity_id'] = $id;
		$data['user_id'] = get_user();
		$acti_log = $logModel->where($data)->find();
		//如果存在记录，则修改记录的状态
		if ($acti_log) {
			if($acti_log['status'] == 11){
				$this->ajaxReturn(array('status'=>1,'message'=>'已成功报名'));
			}
			if($activi['join_ed'] > 0){
				$this->ajaxReturn(array('status'=>0,'message'=>'报名已关闭'));
			}
			//将已有状态改为11，显示为已报名成功，等待邀约
			$res =  $logModel->where($data)->save(array('status'=>11,'is_invited'=>1));
		}else{
			if($activi['join_ed'] > 0){
				$this->ajaxReturn(array('status'=>0,'message'=>'报名已关闭'));
			}
			//如果没有记录，则添加一条记录
			$data['status'] = 11;
			$data['add_time'] = $now;
			$data['is_invited'] = 1;
			$data['last_time'] = $activi['start_time'];
			$res = M('activity_log')->add($data);
		}
		if ($res === false) {
			$this->ajaxReturn(array('status'=>0,'message'=>'报名失败'));
		}
		//报名的人数加一
		$activiModel->where('id='.$id)->setInc('bao_ed',1);
		$fa_username = M('user')->where('id='.$activi['user_id'])->getField('username');
		//成功报名通知
		$content = "您已成功报名".$fa_username."的活动，请耐心等待邀约！";
		D('Message')->addSysMsg($user_id,$content,$id);
		$content = $user["username"]."报名了你的活动";
		D('Message')->addSysMsg($activi['user_id'],$content,$id);
		//分配红包
		D('RedMoney')->addRedMoney($id,$user_id,$activi['join_money'],$activi['start_time']);
		if ($todayTimes) {
			if ($todayTimes['add_time'] == strtotime(date("Y-m-d"))) {
				M('today_times')->where('user_id='.$user_id)->setInc('bao_times',1);
			}else{
				$saveArray = array('add_time'=>strtotime(date('Y-m-d')),'abolish_times'=>0,'fa_times'=>0,'bao_times'=>1);
				M('today_times')->where('user_id='.$user_id)->save($saveArray);
			}
		}else{
			//如果没有记录，则添加一条数据
			$todayData = array(
				'user_id'	=>$user_id,
				'fa_times'	=>0,
				'bao_times'	=>1,
				'abolish_times' => 0,
				'add_time' 	=>strtotime(date("Y-m-d"))
			);
			M('today_times')->add($todayData);
		}
		M('follow')->where("(user_id=".$user_id." AND fo_user_id=".$activi['user_id'].") OR (user_id=".$activi['user_id']." AND fo_user_id=".$user_id.")")->save(array('is_friend' => 1));
		$this->ajaxReturn(array('status'=>1,'message'=>'等待对方邀约'));
	}
	//取消参加
	public function cancelJoin(){
		//没有活动id返回参数错误
		$id = I('post.id') ? I('post.id') : $this->ajaxReturn(array('status'=>-1,'message'=>'参数错误'));
		$user_id = get_user() ? get_user() : $this->ajaxReturn(array('status'=>-1,'message'=>'参数错误'));
		$logModel = M('activity_log');
		$acti_log = $logModel->where(array('activity_id'=>$id,'user_id'=>$user_id))->find();
		if (!$acti_log) {
			//找不到记录，返回参数错误
			$this->ajaxReturn(array('status'=>-1,'message'=>'您还未参加过此活动'));
		}
		if ($acti_log['status'] == 23) {
			$this->ajaxReturn(array('status'=>1,'message'=>'您已取消参加该活动'));
		}
		//判断记录的转态值是不是11，是11的话就修改为12表示退出，不能再操作
		if ($acti_log['status'] != 11) {
			$this->ajaxReturn(array('status'=>0,'message'=>'参数错误,请刷新'));
		}
		$activi = M('activity')->where('id='.$id)->find();
		if($activi['start_time'] < time()){
			$this->ajaxReturn(array('status'=>0,'message'=>'取消参加活动只能在活动开始前'));
			exit;
		}
		$res = $logModel->where(array('activity_id'=>$id,'user_id'=>$user_id))
				->save(array('status'=>23,'is_invited'=>1));
		if ($res === false) {
			$this->ajaxReturn(array('status'=>0,'message'=>'取消失败'));
		}
		
		M('activity')->where('id='.$id)->setDec('bao_ed',1);
		$userModel = M('user');
		//将用户抢到的红包返还给活动发起者
		M('red_money')->where(array('user_id'=>$user_id,'activity_id'=>$id))->save(array('status'=>-1));
		/*$logdata = array(
			'user_id' => $activi['user_id'],
			'money'  => $activi['join_money'],
			'add_time' => time(),
			'type'   => 105,
			'pay_type' => 3,
			'activity_id' => $id,
			'content'  => '用户取消参与活动退回红包'
 		);
		M('money_log')->add($logdata);*/
		//$userModel->where('id='.$activi['user_id'])->setInc('money',$activi['join_money']);
		$username1 = $userModel->where('id='.$activi['user_id'])->getField('username');
		$username2 = $userModel->where('id='.$user_id)->getField('username');
		//分别添加记录和消息通知
		$msgModel = D('Message');
		$content1 = "您已取消参加".$username1."的活动，系统将自动把红包返还给他";
		$content2 = $username2."取消参加你的活动";

		$msgModel->addSysMsg($user_id,$content1);
		$msgModel->addSysMsg($activi['user_id'], $content2, $id);
		cancelFriend($user_id,$activi);
		$this->ajaxReturn(array('status'=>1,'message'=>'取消成功'));
	}

	//刷脸领取见面礼
	public function getGift(){
		$id = I('id');
		$user_id = get_user();
		if($_POST){
			if (!$id) {
				$this->ajaxReturn(array('status'=>0,'message'=>'参数错误'));
			}
			if (!$user_id) {
				$this->ajaxReturn(array('status'=>0,'message'=>'请先登录'));
			}
		}else{
			if (!$id) {
				$this->error('参数错误');
			}
			if (!$user_id) {
				$this->error('请先登录');
			}
		}
		
		$actiModel = M('activity');
		$activi = $actiModel->where('id='.$id)->find();
		$username = M('user')->where('id='.$user_id)->getField('username');
		$gift_log = M('activity_log')->where(array('activity_id'=>$id,'user_id'=>$user_id))->find();
		if(!$gift_log){
			if($_POST){
				$this->ajaxReturn(array('status'=>0,'message'=>'您没有参与该活动'));
			}else{
				$this->error('您没有参与该活动');
			}
		}
		if($gift_log['is_gift'] == 1){
			//查看红包是否已经领取
			$map = array('user_id'=>$user_id,'activity_id'=>$id,'type'=>2);
			$is_gift = M('red_money')->where($map)->find();
			if(!$is_gift){//如果不存在见面礼红包
				D('RedMoney')->getGift($id,$user_id,$activi['gift']);
			}
			if($_POST){
				$this->ajaxReturn(array('status'=>2,'message'=>'红包已领取，请到钱包查看','money'=>$activi['gift']));
			}else{
				$this->success('红包已领取，请到钱包查看');
			}
		}
		if($gift_log['is_arrive'] == 0){
			if($_POST){
				$this->ajaxReturn(array('status'=>0,'message'=>'您好没有签到，请先签到'));
			}else{
				$this->error('您好没有签到，请先签到');
			}
		}
		//将活动记录中的领取红包字段改成1
		//if($activi['status'] == 9){
			//$res = M('activity_log')->where(array('activity_id'=>$id,'user_id'=>$user_id))
			//	->save(array('status'=>16,'is_gift'=>1));
		//}else{
			$res = M('activity_log')->where(array('activity_id'=>$id,'user_id'=>$user_id))
				->save(array('status'=>16,'is_gift'=>1));
			$actiModel->where('id='.$activi['id'])->save(array('status'=>9));
		//}
		//添加见面礼红包
		D('RedMoney')->getGift($id,$user_id,$activi['gift']);
		$content = $username.'已领取您的见面礼红包';
		D('Message')->addSysMsg($activi['user_id'],$content,$id);
		if($res === false){
			$this->error('领取红包失败');
		}
		$actiModel->where('id='.$id)->save(array('is_gift'=>1));
		if($_POST){
			$this->ajaxReturn(array('status'=>1,
				'message'=>'红包已领取，请到钱包查看',
				'money'=>$activi['gift']));
		}else{
			$this->error('红包已领取，请到钱包查看');
		}
	}
	//确认完成活动
	public function finish(){
		$id = I('post.id');
		$user_id = get_user();
		if (!$id || !$user_id) {
			$this->ajaxReturn(array('status'=>0,'message'=>'参数错误'));
		}
		M('activity')->where('id='.$id)->save(array('status'=>9));
		$log_where = array(
			'activity_id'=>$id,
			'is_invited'=>2
		);
		$res = M('activity_log')->where($log_where)->save(array('status'=>16));
		M('red_money')->where(array('activity_id'=>$id,'status'=>0))->save(array('get_time',time()+24*3600));
		if ($res === false) {
			$this->ajaxReturn(array('status'=>0,'message'=>'确认完成失败'));
		}
		$this->ajaxReturn(array('status'=>1,'message'=>'确认完成成功'));
	}
	//延迟
	public function delay(){
		$delay_time = strtotime(I('post.delay_time'));
		$id = I('post.id');
		if (!$id || !is_numeric($delay_time)) {
			$this->ajaxReturn(array('status' => -1,'message' => '参数错误'));
		}
		$start_time = M('activity')->where('id='.$id)->getField('start_time');
		if($start_time < time()){
			$this->ajaxReturn(array('status'=>0,'message'=>'活动已经过期，不能再延期'));
		}
		$res = M('activity')->where('id='.$id)->save(array('status'=>5,'start_time'=>$delay_time,'is_delay'=>2));
		if ($res === false) {
			$this->ajaxReturn(array('status'=>0,'message'=>'延迟失败'));
		}
		$this->ajaxReturn(array('status'=>1,'message'=>'延迟成功'));
	}
	//取消延迟
	public function notDelay(){
		$id = I('post.id');
		if (!$id) {
			$this->ajaxReturn(array('status'=>-1,'message'=>'参数错误'));
		}
		$res = M('activity')->where('id='.$id)->save(array('status'=>4,'is_delay'=>1));
		if ($res === false) {
			$this->ajaxReturn(array('status'=>0,'message'=>'取消延迟失败'));
		}
		$this->ajaxReturn(array('status'=>1,'message'=>'取消延迟成功'));
	}
	//取消活动，已经有报名者，还是确定要取消，发出的红包无法退回，退还见面礼红包给发起者，已经发出的24小时之后自动兑换给参与者
	//当时还没有参与者，取消之后红包和见面礼红包退还给自己
	//已经选择了报名用户，24小时之内第一次取消，发出红包无法退回
	//第二次取消，平台扣下20%的见面礼红包。
	public function abolish(){
		$id = I('post.id');
		$user_id = get_user();
		if (!$id || !$user_id) {
			$this->ajaxReturn(array('status'=>0,'message'=>'参数错误'));
		}
		$actiModel = M('activity'); $userModel = M('user');$logModel = M('activity_log');
		$activi = $actiModel->where('id='.$id)->find();

		if(1 == $activi['is_remove']){
			$this->ajaxReturn(array('status'=>0,'message'=>'活动已经取消，重复操作'));
		}
		$user = $userModel->field("id,username")->where('id='.$user_id)->find();
		//将活动状态值改为已取消,状态值为1
		$res = $actiModel->where('id='.$id)->save(array('status'=>1,'is_remove'=>1,'remove_time'=>time()));
		//如果还没有人报名，红包和见面礼退还给发起人
		//检查是否有过取消活动
		//查看用户是否报名次数达到5次
		$todayTimes = M('today_times')->where('user_id='.$user_id)->find();
		//如果有记录，则查看今天的情况
		if ($todayTimes) {
			if($todayTimes['add_time'] == strtotime(date("Y-m-d"))){
				if ($todayTimes['abolish_times'] >= 3) {
					$this->ajaxReturn(array('status'=>0,'message'=>'每天只能取消三次活动，当前活动不能取消'));
					exit;
				}
			}
		}
		if($activi['start_time'] < time()){
			$this->ajaxReturn(array('status'=>0,'message'=>'活动已经开始，不可取消'));
			exit;
		}
		//if ($activi['bao_ed'] == 0) {
			//报名红包+见面礼
			$sum = $activi['join_money'] * ($activi['bao_num'] - $activi['bao_ed']) + $activi['gift'];
			//将钱返回给用户钱包
			$userModel->where('id='.$user_id)->SetInc('money',$sum);
			//添加金钱记录
			D('MoneyLog')->addCancelActiviMoneyLog($user_id,$sum,$id);
		//}


		$log = $logModel->where('activity_id='.$activi['id'])->select();
		foreach ($log as $key => $value) {
			if($value['is_invited'] > 0 || $value['status'] != 23){
				$content = "非常抱歉，您报名活动已被".$user['username']."取消。您还可以看看其他的活动";
				cancelFriend($value['user_id'],$activi);
				D('Message')->addSysMsg($value['user_id'],$content,$value['activity_id']);
			}
			if($value['status'] != 2 && $value['status'] !=1){
				$logModel->where('id='.$value['id'])->save(array('status'=>6));
			}
		}

		if ($res === false) {
			$this->ajaxReturn(array('status'=>0,'message'=>'取消失败'));
		}
		//如果存在操作记录
		if($todayTimes){
			if($todayTimes['add_time'] == strtotime(date('Y-m-d'))){
				//您已取消活动，
				$content = '您已取消活动，每天最多能取消3次活动，这是您今天第'.($todayTimes['abolish_times']+1).'次取消活动';
				D('Message')->addSysMsg($user_id,$content,$id);
				M('today_times')->where('user_id='.$user_id)->setInc('abolish_times',1);
			}else{
				$content = '您已取消活动，每天最多能取消3次活动，这是您今天第1次取消活动';
				D('Message')->addSysMsg($user_id,$content,$id);
				$saveArray = array('add_time'=>strtotime(date('Y-m-d')),'abolish_times'=>1,'fa_times'=>0,'bao_times'=>0);
				M('today_times')->where('user_id='.$user_id)->save($saveArray);
			}
		}else{
			$saveArray = array('user_id'=>$user_id,'add_time'=>strtotime(date('Y-m-d')),'abolish_times'=>1,'fa_times'=>0,'bao_times'=>0);
			$content = '您已取消活动，每天最多能取消3次活动，这是您今天第1次取消活动';
				D('Message')->addSysMsg($user_id,$content,$id);
			M('today_times')->add($saveArray);
		}
		$this->ajaxReturn(array('status'=>1,'message'=>'取消成功'));
	}

	//确认参与人
	public function confirm(){
		$id = I('post.id');
		$user_ids = I('post.user_ids');
		if (count($user_ids) == 0) {
			$result['status'] = 0;
			$result['message'] = '请选择要邀约的人';
			$this->ajaxReturn($result);
		}

		$logModel = M('activity_log');
		$actiModel = M('activity');
		$activity = $actiModel
					->field("id,bao_ed,user_id,cate,people,start_time,join_money,status")
		       	    ->where('id='.$id)->find();
		$me = M('user')->where('id='.$activity['user_id'])->find();
		//对比后台设置的允许人数和自己邀约的人数，如果大于后台设置的人数，则返回
		if ($activity['people'] <= count($user_ids)) {
			$this->ajaxReturn(array('status'=>-1,'message'=>"邀约人数过多，请重新选择"));
		}
		//如果只邀约了一个人
		if (count($user_ids) == 1) {
			$where = array(
				'activity_id'=>$id,
				'user_id' => $user_ids[0],
				'status' => 11,
				'is_invited' =>1 
			);

			$list = $logModel->where($where)->find();

			if(!$list){
				$this->ajaxReturn(array('status'=>-1,'message'=>"确定的人中有人被其他活动邀约了"));
			}
			//将被邀约的人的转态改成14，is_invited值改成2
			$res = $logModel->where($where)->save(array('status'=>14,'is_invited'=>2));
			//将活动的状态值改成7，等待签到
			$actiModel->where('id='.$id)->save(array('status'=>7,'join_ed'=>1));
			if ($res === false) {
				$this->ajaxReturn(array('status'=>0,'message'=>'失败'));
			}
			//查看是否还报名了其他活动，若有报名，将报名则从报名列表中删除
			//先判断现在是哪一个时间段，是早上6点以前，还是6点以后
			$map['a.user_id'] = $user_ids[0];
			$map['a.is_invited'] = 1;
			if (time()< strtotime(date("Y-m-d 6:00:00"))) {
				//如果是在早上6点以前，则报名开始时间要在现在以后和6点以前
				$map['b.start_time'] = array('between',time().','.strtotime(date("Y-m-d 6:00:00")));
			}else{
				//入股是早上6点以后，则开始时间要大于现在，并并且小于明早
				$map['b.start_time'] = array('between',time().','.strtotime(date("Y-m-d",strtotime("+1 day"))));
			}
			$otherlog =  M('activity_log a')->field('a.user_id,a.id,a.activity_id')
			                ->join("left join zm_activity b on a.activity_id=b.id")
			                ->where($map)->select();
			if ($otherlog) {
				foreach ($otherlog as $other_key => $other_value) {
					//将活动的状态设置成报名关闭，被邀请设置成0
					M('activity_log')->where('id='.$other_value['id'])->save(array('status'=>10,'is_invited'=>0,'not_join'=>1));
					//将其从发布者活动列表中移除
					M('activity')->where('id='.$other_value['activity_id'])->setDec('bao_ed',1);
					//将报名红包还给别人
					M('red_money')->where(array('user_id'=>$other_value['user_id'],'activity_id'=>$other_value['activity_id']))->save(array('status'=>0));
				}
			}
			//系统向用户发送消息，告知他已经被邀约参加活动，请准时到达
			$content = $me['username']."已邀约您参与他的活动，请准时到达";
			D('Message')->addSysMsg($user_ids[0],$content,$id);
			$this->ajaxReturn(array('status'=>1,'message'=>'确认成功'));
		}else{
			//先查找是否有人被邀约了
			$where = array(
				'user_id' => array('in',$user_ids),
				'is_invited' =>2 
			);
			$is_join = M('activity_log')->where(array('user_id'=>array('in',$user_ids),'is_invited'=>2))->find();
			if ($is_join) {
				//如果有人被邀约，则返回失败
				$this->ajaxReturn(array('status'=>-1,'message'=>"确定的人中有人被其他活动邀约了"));
			}
			foreach ($user_ids as $user_k => $user_v) {
				$where = array(
					'activity_id'=>$id,
					'user_id' => $user_v,
					'status' => 11,
					'is_invited' =>1 
				);
				$list = $logModel->where($where)->find();
				if(!$list){
					continue;
				}
				//将被邀约的人的转态改成14，is_invited值改成2
				$res = $logModel->where($where)->save(array('status'=>14,'is_invited'=>2));
				if ($res === false) {
					$this->ajaxReturn(array('status'=>0,'message'=>'失败'));
				}
				//查看是否还报名了其他活动，若有报名，将报名则从报名列表中删除
				//先判断现在是哪一个时间段，是早上6点以前，还是6点以后
				$map['a.user_id'] = $user_v;
				$map['a.is_invited'] = 1;
				if (time()< strtotime(date("Y-m-d 6:00:00"))) {
					//如果是在早上6点以前，则报名开始时间要在现在以后和6点以前
					$map['b.start_time'] = array('between',time().','.strtotime(date("Y-m-d 6:00:00")));
				}else{
					//入股是早上6点以后，则开始时间要大于现在，并并且小于明早
					$map['b.start_time'] = array('between',time().','.strtotime(date("Y-m-d",strtotime("+1 day"))));
				}
				$otherlog =  M('activity_log a')->field('a.user_id,a.id,a.activity_id,')
				                ->join("left join activity b on a.activity_id=b.id")
				                ->where($map)->select();
				if ($otherlog) {
					foreach ($otherlog as $other_key => $other_value) {
						//将活动的状态设置成报名关闭，被邀请设置成0
						M('activity_log')->where('id='.$other_value['id'])->save(array('status'=>10,'is_invited'=>0,'not_join'=>1));
						//将其从发布者活动列表中移除
						M('activity')->where('id='.$other_value['activity_id'])->setDec('bao_ed',1);
						//将报名红包还给别人
						M('red_money')->where(array('user_id'=>$other_value['user_id'],'activity_id'=>$other_value['activity_id']))->save(array('status'=>0));
					}
				}
			}
			$this->ajaxReturn(array('status'=>1,'message'=>'确认成功'));
		}
	
	}

	//希望被邀约时间完成
	public function invite_time(){
		$id = I('post.id');
		$user_id = get_user();
		$invite_time = strtotime(I('invite_time'));
		if (!$id || !$user_id || $invite_time) {
			$this->ajaxReturn(array('status'=>0,'message'=>'参数错误'));
		}
		$res = M('activity_log')->where(array('user_id'=>$user_id,'activity_id'=>$id))->save(array('last_time'=>$invite_time));
		if ($res === false) {
			$this->ajaxReturn(array('status'=>0,'message'=>'确认邀约时间失败'));
		}
		$this->ajaxReturn(array('status'=>1,'message'=>'确认邀约时间成功'));
	}
	//退出活动,从参与人中被删除，通知发起人，发起人参与活动人出减一，自己的转态变为12，
	//领取的红包要还回来，查找红包记录，
	//记录在一个表中，以后参加的时候要去查找是否退出过活动，要接受相应惩罚，  
	public function dropOut(){
		$id = I('post.id');
		$user_id = get_user();
		if (!$id || !$user_id) {
			$this->ajaxReturn(array('status'=>0,'message'=>'参数错误'));
		}

		//判断活动是否已经过去
		$activiModel = M('activity');   $activi = $activiModel->where('id='.$id)->find();
		if($activi['start_time'] < time()){
			$this->ajaxReturn(array('status'=>0,'message'=>'活动已经开始，不可退出'));
			exit;
		}

		$userModel = M('user');
		$username = $userModel->where('id='.$user_id)->getField('username');
		$log_where = array('user_id'=>$user_id,'activity_id'=>$id);
		$log = M('activity_log')->where($log_where)->find();
		if($log['status'] == 12){$this->ajaxReturn(array('status'=>0,'message'=>'您已退出过活动'));}
		$res = M('activity_log')->where('id='.$log['id'])->save(array('status'=>12,'is_invited'=>0));
		if ($res === false) {
			$this->ajaxReturn(array('status'=>0,'message'=>'退出活动失败'));
		}
		
		$join_ed = $activi['join_ed'] - 1; $bao_ed = $activi['bao_ed'] - 1;
		$activiModel->where('id='.$id)->save(array('join_ed'=>$join_ed,'bao_ed'=>$bao_ed));
		//退还红包里面的钱
		$redModel = M('red_money');
		$redModel->where(array('activity_id'=>$id,'user_id'=>$user_id))->save(array('status'=>-1,'edit_time'=>time()));
		//将抢到的红包退还给活动发起人,并添加记录
		/*$logdata = array(
			'user_id' => $activi['user_id'],
			'money'  => $activi['join_money'],
			'add_time' => time(),
			'type'   => 105,
			'activity_id' => $id,
			'content'  => '用户退出活动退回红包',
			'pay_type' => 3
 		);
		M('money_log')->add($logdata);
		$userModel->where('id='.$activi['user_id'])->setInc('money',$activi['join_money']);*/
		//将过去24小时抢到的红包退还到平台账户上，
		//平台上有三个地方需要修改，平台总金额，平台记录，平台收到的退还红包记录
		$red_where = array(
			'user_id' => $user_id,     //用户id
			'add_time' => array('gt',time()-24*60*60), //24小时以内的
			'activity_id' => array('neq',$id), //其它活动的
			'status' => array('neq',-1),
			'type'	=>1
		);
		//先查找过去24时有没有抢到红包
		$red_money24 = $redModel->where($red_where)->select();
		//如果抢到了
		if ($red_money24) {
			if (count($red_money24) > 1) {
				$red_ids = array();$plat_red = array();$sum = 0;
				$i=0;
				foreach ($red_money24 as $key => $value) {
					$red_ids[$i] = $value['id'];
					$plat_red[$i] = array(
						'user_id' => $user_id,
						'money' => $value['money'],
						'add_time' => time(),
						'activity_id' => $value['activity_id']
					);
					$i++;
				}
				$map = array('id'=>array('in',$red_ids));
				$redModel->where($map)->save(array('status'=>-1,'edit_time'=>time()));
				M('platform_redlog')->addAll($plat_red);
			}else{
				$plat_red = array(
					'user_id' => $user_id,
					'money' => $red_money24[0]['money'],
					'add_time' => time(),
					'activity_id' => $red_money24[0]['activity_id']
				);
				$redModel->where('id='.$red_money24[0]['id'])->save(array('status'=>-1));
				M('platform_redlog')->add($plat_red);
			}
			
		}
		//添加参与人退出活动通知
		if ($activi['join_ed'] == 1) {
			$activiModel->where('id='.$id)->save(array('status'=>11));
			$content = $username."退出了活动，目前被邀约人数为0，活动未完成";
		}else{
			$content = $username."退出了您的活动,活动红包已退回到您的账户";
		}
		D('Message')->addSysMsg($activi['user_id'],$content);
		//添加退出次数,并冻结账户
		$month_drop = M('today_times')->where('user_id='.$user_id)->find();
		if($month_drop['date_m'] == intval(date('m'))){
			if($month_drop['tui_times'] >= 3 ){
				//如果已经退出过三次，永久冻结账户
				M('frize')->where('user_id='.$user_id)->save(array('status'=>2,'to_user_id'=>$activi['user_id'],'is_drop'=>1));
			}else{
				M('frize')->where('user_id='.$user_id)->save(array('status'=>1,'to_user_id'=>$activi['user_id'],'is_drop'=>1));
				//通知其支付诚意金
				D('Message')->sendPayChengyiMessage($user_id);
			}
		}else{
			$frize = M('frize')->where('user_id='.$user_id)->find();
			M('today_times')->where('user_id='.$user_id)->save(array('date_m'=>intval(date('m')),'tui_times'=>1));
			if($frize){
				M('frize')->where('user_id='.$user_id)->save(array('status'=>1,'to_user_id'=>$activi['user_id'],'is_drop'=>1));
			}else{
				$frize_data = array('user_id'=>$user_id,'status'=>1,'to_user_id'=>$activi['user_id'],'is_drop'=>1);
				M('frize')->add($frize_data);
			}
			//通知其支付诚意金
			D('Message')->sendPayChengyiMessage($user_id);
		}
		cancelFriend($user_id,$activi);
		$this->ajaxReturn(array('status'=>1,'message'=>'退出活动成功'));
	}
	public function arrive(){
		$id = I('post.id');
		$user_id = get_user();
		$lat = I('post.lat');$lng = I('post.lng');
		if (!$id || !$user_id || !$lat || !$lng) {
			$this->ajaxReturn(array('status'=>-1,'message'=>'失败'));
		}
		$actiModel = M('activity');
		$activity = $actiModel->where('id='.$id)->find();
		//判断时间是否过了1.5个小时，过了1.5个小时，
		if (time() - $activity['start_time'] > 90*60) {
			$this->ajaxReturn(array('status'=>0,'message'=>'时间已过'));
		}elseif($activity['start_time'] > time()){
			$this->ajaxReturn(array('status'=>0,'message'=>'还未到时间'));
		}else{
			$user = M('user')->where('id='.$user_id)->find();
			$distance = getdistance($lng,$lat,$activity['lng'],$activity['lat']);
			//如果在活动地点的300米以内
			/*if ($distance > 0.4) {
				$this->ajaxReturn(array('status'=>0,'message'=>'您当前未在指定的活动范围区域内，请到达活动区后再确认到达'));
			}*/
			//如果是自己发布的活动
			if ($user_id == $activity['user_id']) {
				$res = $actiModel->where('id='.$id)
						->save(array('status'=>13,'is_arrive'=>1,'arrive_lat'=>$lat,'arrive_lng'=>$lng));
				$urls = 'http://'.$_SERVER['SERVER_NAME'].'/index.php/Home/Activity/getGift?id='.$id;
				$data = addCode($urls,$user_id);
				$rescan = M('activity_log')
						->where(array('activity_id'=>$id,'is_invited'=>2,'is_arrive'=>1))->find();
				if($rescan){
					//当参与人签到了
					$this->getRecRed($rescan['user_id'], $id);
					judgeFriend($activity,$rescan['user_id']);
				}
				$actiModel->where('id='.$id)->save(array('scan_id'=>$data));
				M('dealing')->where('activity_id='.$id)->save(array('fa_arrive'=>1,'fa_msg'=>1));
			}else{
				//如果是参加别人的活动
				$res = M('activity_log')
						->where(array('activity_id'=>$id,'user_id'=>$user_id))
						->save(array('status'=>14,'is_arrive'=>1,'lat'=>$lat,'lng'=>$lng));
				if($activity['is_arrive'] == 1){
					$actiModel->where('id='.$id)
							->save(array('status'=>13));

					$this->getRecRed($user_id, $id);
					judgeFriend($activity,$user_id);
				}
				M('dealing')->where('activity_id='.$id)->save(array('join_arrive'=>1,'join_msg'=>1));
			}
			if ($res === false) {
				$this->ajaxReturn(array('status'=>0,'message'=>'确认到达失败'));
			}
			
			$this->ajaxReturn(array('status'=>1,'message'=>'确认到达成功'));
		}
	}

	public function getRecRed($user_id, $activity_id){
		$tui_id = M('user')->where(array('id' => $user_id))->getField('tui_id');

		if($tui_id){
			$tui_money = M('admin_setting')->where('id=1')->getField('tui_red');
			$red = array(
				'activity_id'=> $activity_id,
				'user_id' => $tui_id,
				'money'	  => $tui_money,
				'type'	  => 3,
				'status'  => 1,
				'content' => '推荐红包',
				'add_time'=> time(),
				'get_time'=> (time() + 24*3600)
			);

			M('red_money')->add($red);
		}
	}

	 /**
     * 添加数据
     */
    public function addData($model = FALSE) {
        if (!$model) {
        	$result['status'] = 0;
        	$result['message'] = '参数错误';
            $this->ajaxReturn($result);
        }
        if ($model->create()) {
            $result['id'] = $model->add();
            $result['status'] = 1;
            $result['message'] = '发布成功';
            $this->ajaxReturn($result);
        } else {
        	$result['status'] = 0;
            $result['message'] = $model->getError();
            $this->ajaxReturn($result);
        } 
    }
    //获取粉丝
	public function getFans(){
		$user_id = I('post.user_id') ? I('post.user_id') : get_user();
		if (!$user_id) {
			$this->ajaxReturn(array('status'=>0,'message'=>'请求失败'));
		}
		$followModel = M('follow');
		$list = M('follow a')
	            ->field("b.id as user_id,b.username,b.sex,b.photo_id,b.login_time,b.first_str,b.introduction,b.lat,b.lng,a.user_id as auid")
	            ->join("left join zm_user b on a.user_id=b.id")
	            ->where('fo_user_id='.$user_id. ' AND b.id IS NOT NULL')
	            ->order("first_str asc")
	            ->select();
 		//$total = $followModel->where('fo_user_id='.$user_id. ' AND b.id IS NOT NULL')->count();
 		$follows = $followModel->where('user_id='.$user_id)->getField('fo_user_id',true);
 		$me = M('user')->where('id='.$user_id)->field("id,lng,lat,login_time,introduction")->find();
 		$data = array();
 		if ($list) {
 			foreach ($list as $key => $value) {
 				if(empty($value['user_id']))
 					continue;
 				//验证是否互相关注
 				if (in_array($value['user_id'], $follows)) {
 					$list[$key]['is_friend'] = 1;
 				}else{
 					$list[$key]['is_friend'] = 0;
 				}
 				$value['last_login'] = getLoginTime($value['login_time']);
 				$value['distance'] = getdistance($me['lng'],$me['lat'],$value['lng'],$value['lat']);
 				$list[$key]['last_login'] = $value['last_login'];
 				$list[$key]['distance'] = $value['distance'];
 				
 				$data[] = $list[$key];
 			}
 			$total = count($data);
 			$this->ajaxReturn(array('status'=>1,'message'=>'请求成功','total'=>$total,'list'=>$list));
 		}
 		$this->ajaxReturn(array('status'=>0,'message'=>'请求失败','total'=>0,'list'=>array()));
	}
	//获取关注者
	public function getFollows(){
		$user_id = I('post.user_id') ? I('post.user_id') : get_user();
		if (!$user_id) {
			$this->ajaxReturn(array('status'=>0,'message'=>'请求失败'));
		}
		$followModel = M('follow');
		$list = M('follow a')
		          ->field("b.id as user_id,b.username,b.sex,b.photo_id,b.login_time,b.first_str,b.introduction,b.lat,b.lng")
                  ->join("left join zm_user b on a.fo_user_id=b.id")
		          ->where('user_id='.$user_id. ' AND b.id IS NOT NULL')
		          ->order("first_str asc")->select();
 		$total = $followModel->where('user_id='.$user_id)->count();
 		$fans = $followModel->where('fo_user_id='.$user_id)->getField('user_id',true);
 		$me = M('user')->where('id='.$user_id)->field("id,lng,lat,login_time,introduction")->find();
 		$returnList = array();
 		$data = array();
 		if ($list) {
 			//组装数组
 			foreach ($list as $key => $value) {
 				if(empty($value['user_id']))
 					continue;
 				//验证是否互相关注
 				if (in_array($value['user_id'], $fans)) {
 					$list[$key]['is_friend'] = 1;
 				}else{
 					$list[$key]['is_friend'] = 0;
 				}
 				$list[$key]['last_login'] = getLoginTime($value['login_time']);
 				$list[$key]['distance'] = getdistance($me['lng'],$me['lat'],$value['lng'],$value['lat']);
 				$data[] = $list[$key];
 			}
 			$total = count($data);
 			$this->ajaxReturn(array('status'=>1,'message'=>'请求成功','total'=>$total,'list'=>$list));
 		}
 		$this->ajaxReturn(array('status'=>0,'message'=>'请求失败','total'=>0,'list'=>array()));
 		
	}

	public function getContact(){
		$user_id = I('post.user_id') ? I('post.user_id') : get_user();
		$conModel = M('contact');
		$kefu_flag = false;
		if($user_id == 3){
			$where['id'] = array('neq',3);
			$where['is_verify'] = 1;
			$data = M('user')->field('id as user2,username,sex,photo_id,login_time,first_str,introduction,lat,lng')->where($where)->select();
			$kefu_flag = true;
		}else{
			$data1 = M('contact a')->field('a.user2,b.username,b.sex,b.photo_id,b.login_time,b.first_str,b.introduction,b.lat,b.lng')
		                 ->where('user1='.$user_id)
		                 ->join('left join zm_user b on a.user2=b.id')
		                 ->select();
			$data2 = M('follow a')
			          ->field("b.id as user2,b.username,b.sex,b.photo_id,b.login_time,b.first_str,b.introduction,b.lat,b.lng")
	                  ->join("left join zm_user b on a.fo_user_id=b.id")
			          ->where('user_id='.$user_id.' AND is_friend=1')
			          ->order("first_str asc")->select();
			$data = array_merge($data1,$data2);
		}
		

		
		$me = M('user')->where('id='.$user_id)->field("id,lng,lat,login_time,introduction")->find();
		$ids = array();
		$list = array();
		
		foreach ($data as $key => $value) {
			if($value['user2'] == 3)
				$kefu_flag = true;
			if(in_array($value['user2'], $ids)){
				continue;
			}else{
				array_push($ids, $value['user2']);
			}
			$data[$key]['user_id'] = $value['user2'];
			$data[$key]['last_login'] = getLoginTime($value['login_time']);
 			$data[$key]['distance'] = getdistance($me['lng'],$me['lat'],$value['lng'],$value['lat']);
 			$list[] = $data[$key];
		}
		if($kefu_flag == false)
		{
			$kefu = M('user')->field('id as user_id,username,sex,photo_id,login_time,first_str,introduction,lat,lng')->where('id=3')->find();
			$kefu['user2'] = $kefu['user_id'];
			$kefu['first_str'] = 'K';
			$kefu['distance'] = getdistance($me['lng'],$me['lat'],$kefu['lng'],$kefu['lat']);
			$kefu['last_login'] = getLoginTime($kefu['login_time']);
			$list[] = $kefu;
		}
		$total = count($list);
		if($list){
			$this->ajaxReturn(array('status'=>1,'message'=>'请求成功','total'=>$total,'list'=>$list));
		}
		$this->ajaxReturn(array('status'=>0,'message'=>'请求失败','total'=>$total,'list'=>array()));
	}
	 /**
     * 判断是否有人爽约了
     * @access public
     * @param int $user_id 用户id
     * @param int $type 类别，
     * @param int $activity_id 活动id
     * @return bool $activity['id']
     */
	public function isFailJoin($activi){
		$actiModel = M('activity');
		//$activi = $actiModel->where('id='.$activi['id'])->find();
		if($activi['status'] == 11){
			return false;
		}
		$logModel = M('activity_log');
		//查找有没有邀约用户
		$logWhere = array('activity_id'=>$activi['id'],'is_invited'=>2);
		$actiLog = $logModel->where($logWhere)->select();
		if (!$actiLog && $activi['start_time'] < time()) {//如果没有邀约，则直接显示过期
			$actiModel->where('id='.$activi['id'])->save(array('status'=>0));
			return false;
		}
		$status = $logData[0]['status'];
		if($status == 18 || $status == 16){
			return false;
		}
		
		$impressModel = M('impress');
		//证明时间已经过去，查看是否爽约和进行爽约处理
		if ($activi['start_time'] < (time()-90*60)) {
			//查看被邀约者状态，是否也是爽约
			//发起者爽约了
			if ($activi['status'] == 7 && $activi['is_arrive'] == 0) {
				//判断参与者是否也爽约
				foreach ($actiLog as $key => $value) {
					if($value['is_arrive'] == 0){  
						//如果被邀约也爽约，退回报名红包,显示爽约
						$this->canFailJoin($activi,$value['user_id'],true);
						$money = ($activi['bao_num'] * $activi['bao_ed']) + $activi['gift'];
						M('user')->where('id='.$activi['user_id'])->setInc('money',$money);
						$impress = $impressModel->where("user_id=".$value['user_id'])->find;
						if($impress){
							$impressModel->where("user_id=".$value['user_id'])->setInc('fail_join',1);
						}
						break;
					}
					//被邀约者没有爽约，3小时后获得50%见面礼红包，
					$money = $activi['gift'] * 0.5;
					D('MoneyLog')->addFailjoinChengyijing($activi['id'],$value['user_id'],$activi['user_id'],$money);
					$content = '由于您爽约，我们已将50%见面礼的金额作为诚意金，发给被邀约者。';
					D('Message')->addSysMsg($activi['user_id'],$content,$activi['id']);
					$impress = $impressModel->where("user_id=".$activi['user_id'])->find;
					if($impress){
						$impressModel->where("user_id=".$activi['user_id'])->setInc('fail_join',1);
					}
				}
				$actiModel->where('id='.$activi['id'])->save(array('status'=>11));
			}
			if($activi['status'] == 13 && $activi['is_arrive'] == 1 && $actiLog[0]['is_arrive'] == 0){
				foreach ($actiLog as $key => $value) {
					$money = ($activi['join_money'] * ( 1 + $activi['bao_num'] - $activi['bao_ed'])) + $activi['gift'];
					$this->returnHalfGift($activi['id'], $activi['user_id'], $money);
					$impress = $impressModel->where("user_id=".$value['user_id'])->find;
					if($impress){
						$impressModel->where("user_id=".$value['user_id'])->setInc('fail_join',1);
					}
				}
			}
			return true;
		}
		return false;
	}
    public function returnHalfGift($id, $user_id, $money){
		$moneyArray = array(
			'user_id' => $user_id,
			'money'  => $money,
			'add_time' => time(),
			'activity_id' => $id,
			'type'		=> 105,
			'content'	=> '爽约红包退回',
			'pay_type'	=> 3,
		);
		M('money_log')->add($moneyArray);
		M('user')->where(array('id'=>$user_id))->setInc('money',$money);
		M('dealing')->where(array('activity_id'=>$id))->delete();
    }
	//参与人爽约活动
	public function canFailJoin($activi,$user_id,$both_fail=false){
		$logModel = M('activity_log');
		$logData = $logModel->where(array('activity_id'=>$activi['id'],'user_id'=>$user_id))->find();
		if($logData['status'] == 18){return false;}
		//将状态设置成违约
		$res = $logModel->where(array('id'=>$logData['id']))->save(array('status'=>18));
		
		M('red_money')->where(array('activity_id'=>$activi['id'],'user_id'=>$user_id))->save(array('status'=>-1));
		
		$monLog = M('money_log')->where(array('activity_id'=>$activi['id'],'user_id'=>$activi['user_id']))->find();
		if($monLog){return false;}
		
		//增加账户余额
		M('user')->where('id='.$activi['user_id'])->setInc('money',$activi['join_money']);
		if($both_fail == true){
			//将红包退回给发起人
			$log_data = array(
				'user_id' 	=> $activi['user_id'],
				'money'	 	=> $activi['join_money'],
				'add_time'	=> ($activi['start_time']+90*60),
				'type'		=> 105,
				'status'	=> 1,
				'pay_type'	=> 3,
				'activity_id'=> $activi['id'],
				'content'	=> '爽约红包退回'	
			);
			M('money_log')->add($log_data);
			return true;
		}
		//将用户暂时冻结
		$failData = array(
			'user_id' => $user_id,
			'add_time' => time(),
			'activity_id' => $activi['id'],
			'type'		=> 2,
			'year'		=> date('Y'),
			'yue'		=> date('m'), 
		);
		M('fail_join')->add($failData);
		$failTimes = M('fail_join')->where(array('user_id'=>$user_id,'year'=>$failData['year'],'yue'=>$failData['yue']))->count();
		$exist_frize = M('frize')->where('user_id='.$user_id)->find();
		
		if ($failTimes == 3) {
			if ($exist_frize) {
				M('frize')->where('id='.$exist_frize['id'])->save(array('status'=>2,'to_user_id'=>$activi['user_id'],'type' => 1));

			}else{
				M('frize')->add(array('user_id'=>$user_id,'status'=>2,'to_user_id'=>$activi['user_id'],'type' => 1));
			}
			$content = "由于本月已经爽约三次，账号已被永久冻结";
			D('Message')->addSysMsg($user_id,$content);
		}else{
			if ($exist_frize) {
				M('frize')->where('id='.$exist_frize['id'])->save(array('status'=>1,'to_user_id'=>$activi['user_id'],'type' => 1));
			}else{
				M('frize')->add(array('user_id'=>$user_id,'status'=>1,'to_user_id'=>$activi['user_id'],'type' => 1));
			}
			//通知用户支付诚意金
			D('Message')->sendPayChengyiMessage($user_id);
		}
	}

	public function getRedMoney(){
		$id = I('post.id');
		$user_id = get_user();
		$red_money = M('activity')->where('id='.$id)->getField('join_money');
		M('activity_log')->where(array('activity_id'=>$id,'user_id'=>$user_id))
		    ->save(array('get_red'=>1));
		$this->ajaxReturn(array('status'=>1,'message'=>'成功','join_money'=>$red_money,'av_money'=>$red_money));
	}

	public function getDistance(){
		$id = I('id');
		$lat = I('lat');
		$lng = I('lng');
		if(!$id || !$lat || !$lng){
			$this->ajaxReturn(array('status'=>0,'message'=>'参数错误！'));
		}
		$activi = M('activity')->field('lat,lng')->where('id='.$id)->find();
		$distance = getdistance($activi['lng'],$activi['lat'],$lng,$lat);
		if ($distance > 0.4) {
			$this->ajaxReturn(array('status'=>0,'message'=>'您还未到达活动地点300米以内，请到达活动地点后再进行签到！','distance'=>$distance));
		}
		$this->ajaxReturn(array('status'=>1,'message'=>'您已经到达活动地点'));
	}

	public function rongtest(){
		if ($_POST) {
			print_r($_POST);exit;
			//dump(strtotime("2017-3-8 18:30"));
			//$https = "https://".$_SERVER['SERVER_NAME']."/index.php/Home/Activity/detail?&id=".$id."&type=1"."&user_id=".$user_id;
			//dump(date("Y-m-d 18:00:00",strtotime("-1 day")));

			//$abs = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
			//dump(array_search('Z',$abs));
		}
		
		/*$user_id = I('post.user_id') ? I('post.user_id') : $this->ajaxReturn(is_not('user_id/必须'));

		$page_no = I('post.page_no') ? I('post.page_no') : 1;

		$lng = I('post.lng') ? I('post.lng') : '116.492302';
		$lat = I('post.type') ? I('post.type') : '39.976315';

		// 活动ID 数组
		
		$field = 'a.status as join_status,a.is_arrive as join_arrive,a.is_gift as join_gift,b.id,b.status,b.title,b.cate,b.type,b.lng,b.lat,b.theme,b.start_time,b.add_time,b.site,b.right,b.bao_num,b.join_money,b.gift,c.username,c.sex,c.star';
		$list = M('activity_log a')->page($page_no,10)
					->field($field)
					->join('left join zm_activity b on a.activity_id=b.id')
					->join('left join zm_user c on b.user_id=c.id')
					->where(array('a.user_id'=>$user_id,'a.is_invited'=>2))
					->order('b.start_time desc')
					->select();

		foreach ($list as $key => $value) 
		{ 
			$list[$key]['theme'] = explode(',',$value['theme']);
			$list[$key]['gift'] = $value['join_money'] * $value['bao_num'] + $value['gift'];
			$list[$key]['start_time'] = date("Y-m-d H:i:s",$value['start_time']);
			$list[$key]['last_time'] = getIntervalTime($value['add_time']);
			$list[$key]['status_str'] = '报名中';
		}

		$result['photos'] =$photos;
		$result['list']=$list; 

		$this->ajaxReturn($result);*/

		//$data = array('好的'=>1,'不好'=>2);
		//dump($data);exit;

        $this->ajaxReturn(array( getIntervalTime(1500341224)));
		$this->assign('data',$data);
		$this->display();
	}
}